{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 1.两数之和\n",
    "给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出 和为目标值 target  的那 两个 整数，并返回它们的数组下标。\n",
    "\n",
    "你可以假设每种输入只会对应一个答案，并且你不能使用两次相同的元素。\n",
    "\n",
    "你可以按任意顺序返回答案。\n",
    "\n",
    "示例 1：\n",
    "\n",
    "输入：nums = [2,7,11,15], target = 9\n",
    "\n",
    "输出：[0,1]\n",
    "\n",
    "解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。\n",
    "\n",
    "示例 2：\n",
    "\n",
    "输入：nums = [3,2,4], target = 6\n",
    "\n",
    "输出：[1,2]\n",
    "\n",
    "示例 3：\n",
    "\n",
    "输入：nums = [3,3], target = 6\n",
    "\n",
    "输出：[0,1]\n",
    "\n",
    "\n",
    "提示：\n",
    "\n",
    "2 <= nums.length <= 104\n",
    "\n",
    "-109 <= nums[i] <= 109\n",
    "\n",
    "-109 <= target <= 109\n",
    "\n",
    "只会存在一个有效答案\n",
    "\n",
    "\n",
    "进阶：你可以想出一个时间复杂度小于 O(n2) 的算法吗？"
   ],
   "id": "241f0a50696c6fc6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-19T12:45:34.433109Z",
     "start_time": "2025-01-19T12:45:34.424797Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from typing import List\n",
    "\n",
    "'''\n",
    "遍历的做法\n",
    "\n",
    "0,1,2,3,4,5\n",
    "len = 6\n",
    "range(6) = [0,5]\n",
    "index in [0,4]\n",
    "j in [index,5]\n",
    "'''\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    @staticmethod\n",
    "    def twoSum(nums: List[int], target: int) -> List[int]:\n",
    "        \"\"\"\n",
    "        遍历方式\n",
    "        \"\"\"\n",
    "        for i in range(0, len(nums) - 1):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                if nums[i] + nums[j] == target:\n",
    "                    return [i, j]\n",
    "\n",
    "    @staticmethod\n",
    "    def twoSum2(nums: List[int], target: int) -> List[int]:\n",
    "        \"\"\"\n",
    "        字典方式\n",
    "        \"\"\"\n",
    "        hashtable = {}\n",
    "        for i, num in enumerate(nums):\n",
    "            if target - num in hashtable:\n",
    "                return [hashtable[target - num], i]\n",
    "            hashtable[num] = i\n",
    "        return []"
   ],
   "id": "de066d91b4ba562b",
   "outputs": [],
   "execution_count": 1
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
